package com.youle.contoller;

import com.youle.enums.ExceptionEnum;
import com.youle.exception.YLException;
import com.youle.item.pojo.Brand;
import com.youle.item.pojo.ItemCategory;
import com.youle.service.BrandService;
import com.youle.utils.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by
 *
 * @author touni
 * @date 2019/4/18
 * @time 18:53
 */
@RestController
@RequestMapping("brand")
@Api("品牌接口服务")                                             //注明这个controller的目的
public class BrandController {
    @Autowired
    private BrandService brandService;

    @GetMapping("page")
    @ApiOperation(value = "查询品牌的页面",notes = "查询品牌")   //描述一个类或者一个接口
    @ApiImplicitParams({                                    //描述多个请求参数
            @ApiImplicitParam(name = "key",value = "查询的关键字",type = "String"),
            @ApiImplicitParam(name = "page",value = "当前的页数",defaultValue = "1",type = "integer"),
            @ApiImplicitParam(name = "row",value = "一页显示的品牌数",defaultValue = "5",type = "integer"),
            @ApiImplicitParam(name = "sortBy",value = "排序",type = "String"),
            @ApiImplicitParam(name = "desc",value = "描述",type = "Boolean"),
    })
    public ResponseEntity<PageResult> showBrandList(String key,
                                                    @RequestParam(defaultValue = "1")Integer page,
                                                    @RequestParam(defaultValue = "5")Integer row,
                                                    String sortBy,
                                                    Boolean desc){
        return this.brandService.showBrandList(key,page,row,sortBy,desc);
    }

    /**
     * 此处增加和修改功能同一个URL  在service中需要进行判断
     * @param brand
     * @param cids
     * @return
     */
    @RequestMapping("add")
    public ResponseEntity<Void> addBrand( Brand brand,
                                   @RequestParam List<Long> cids){
        return this.brandService.addBrand(brand,cids);
    }


    @GetMapping("bid/{bid}")
    public ResponseEntity<List<ItemCategory>> queryBrandByBid(@PathVariable Long bid){
        if (null == bid){
            throw new YLException(ExceptionEnum.PARAM_NOT_BE_NULL);
        }
        return brandService.queryBrandByBid(bid);
    }

    /**
     * 根据bid来删除brand 和中间表  前端页面未实现 TODO
     * @return
     */
    @RequestMapping("delete/{bid}")
    public ResponseEntity<Void> deleteByBid(@PathVariable Long bid){
        if (null == bid){
            throw new YLException(ExceptionEnum.PARAM_NOT_BE_NULL);
        }
        return brandService.deleteByBid(bid);



    }











}
